import { useConfig } from "@/store/config";
import axios, { AxiosResponse } from "axios";
import { ElNotification } from "element-plus";
import { forceUpdateMP3 } from "./useMP3";

export interface IRequestMsg<T> {
	msg: string;
	code: 200 | 400 | 401 | 402 | 403 | 404 | 500 | 501 | 502 | 503;
	data: T;
}

/**
 *
 * @param url 相对网址
 * @returns 完整网址
 */
export function resolveURL(url?: string | string[]) {
	const { API } = useConfig();
	const suffix = Array.isArray(url) ? url.join("/") : url;
	const address = url ? API + "/" + suffix : API;
	return address.startsWith("http://") ? address : "http://" + address;
}

export async function useRequestGet(
	url?: string | string[],
	params = {},
	timeout = 5000
) {
	const address = resolveURL(url);
	const res = await axios.get(address, {
		params,
		timeout,
	});
	return res;
}

export async function useRequestPost(
	url?: string | string[],
	params = {},

	timeout = 5000
) {
	const address = resolveURL(url);
	const res = await axios.post(address, {
		params,
		timeout,
	});
	return res;
}

export async function useRequestDelete(
	url?: string | string[],
	params = {},
	timeout = 5000
) {
	const address = resolveURL(url);
	const res = await axios.delete(address, {
		params,
		timeout,
	});
	return res;
}

/**
 * 测试连接服务器
 * @returns 连接是否成功
 */
export async function checkServer() {
	try {
		const { data } = (await useRequestGet("hello")) as AxiosResponse<
			IRequestMsg<any>
		>;

		return data?.code === 200 ? "ok" : "err";
	} catch (err: any) {
		if ((err.message as string).includes("timeout")) {
			return "timeout";
		}
		return err.message as string;
	}
}

export interface IMP3Data {
	key: string;
	label: string;
	title: string;
	album: string;
	artist: string;
	tags: string[];
	virtualPath: string;
	mediatype: "AUDIO" | "VIDEO";
}

export function mockIMP3Data(): IMP3Data {
	return {
		key: "",
		tags: [],
		label: "",
		title: "",
		album: "",
		artist: "",
		virtualPath: "",
		mediatype: "AUDIO",
	};
}

export async function fetchMP3List(params = {}) {
	const { data } = (await useRequestGet("media", params)) as AxiosResponse<
		IRequestMsg<Array<IMP3Data>>
	>;

	return data;
}

export async function fetchEditVideoTitle(params = {}) {
	const { data } = (await useRequestGet("media/edit", params)) as AxiosResponse<
		IRequestMsg<Array<IMP3Data>>
	>;

	return data;
}

export async function fetchDeleteItem(params = {}) {
	const { data } = (await useRequestDelete("media", params)) as AxiosResponse<
		IRequestMsg<Array<IMP3Data>>
	>;

	return data;
}

export function deleteItem(key: string) {
	fetchDeleteItem({ key })
		.then(() => {
			ElNotification({
				type: "success",
				message: "删除成功",
			});

			window.location.reload();
		})
		.catch((e) => {
			ElNotification({
				type: "error",
				message: "删除失败，错误代码：" + e,
			});
		});
}

export function forceUpdate(cb?: (p?: any) => any) {
	forceUpdateMP3()
		.then(() => {
			setTimeout(() => {
				ElNotification({
					type: "success",
					message: `正在重新加载数据`,
				});
				if (cb) {
					cb();
				}
			}, 1000);
		})
		.catch((err) => {
			ElNotification({
				type: "error",
				message: `加载数据失败，错误信息：${err}`,
			});
		});
}
